<?php

namespace Common\Model;

/**
 * Description of HotelNoteModel
 * 
 * @author wenhui
 * @date 2017-12-19
 * @index hotelId(`hotelentity_id`)
 * @index noteType(`type`)
 */
class HotelNoteCostModel extends BaseModel {

    protected $trueTableName = 'tb_hotel_note_cost';

    /**
     * 获取客栈记一笔数据
     * @modify wenhui 2017-12-19
     * @param int $hotelId id
     * @param int $noteCostId id
     */
    public function getNoteCost($hotelId, $noteCostId) {
        $option['hotelentity_id'] = $hotelId;
        $option['id'] = $noteCostId;

        $data = $this->where($option)->select();
        return $data;
    }

    /**
     * 添加客栈记一笔数据
     * @modify wenhui 2017-12-19
     * @param array $data 封装好的信息
     * @return boolean          执行结果
     */
    public function addNoteCost($data) {
        $result = $this->add($data);

        return empty($result) ? false : true;
    }

    /**
     * 获取客栈所有分销渠道
     * @modify trainyao 2016-01-08 创建
     * @param  integer   $hotelId   客栈ID
     * @return array        信息数组
     */
    public function getInfoByHotel($hotelId, $type, $startDate, $endDate) {
        if (is_array($hotelId)) {
            $option['hotelentity_id'] = array('in', $hotelId);
        } else {
            $option['hotelentity_id'] = $hotelId;
        }

        if (!is_null($type)) {
            $option['type'] = $type;
        }

        if (!is_null($startDate) && !is_null($endDate)) {
            $option['date'] = array('between', [$startDate, $endDate]);
        }

        $option['isdelete'] = NOT_DELETE;

        $data = $this->where($option)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 编辑记一笔记录
     * @modify wenhui 2017-12-19 创建
     * @param  integer   $hotelId     客栈id
     * @param  integer   $noteCostId     记一笔条目id
     * @param  integer   $noteId     记一笔项目id
     * @param  integer   $payId     支付方式id
     * @param  integer   $type 记一笔记录类型  1=收入 2=支出
     * @param  decimal   $price 金额
     * @param  string   $noteDate 记一笔记录日期
     * @param  string   $remark 详情
     * @param  bool   $isDelete 是否删除
     */
    public function editNoteCost($hotelId, $noteCostId, $noteId, $payId, $type, $price, $noteDate, $remark, $isDelete) {
        $options = array(
            'hotelentity_id' => $hotelId,
            'id' => $noteCostId,
        );

        $data = array();
        if (!is_null($noteId)) {
            $data['hotelnote_id'] = $noteId;
        }
        if (!is_null($payId)) {
            $data['hotelpay_id'] = $payId;
        }
        if (!is_null($type)) {
            $data['type'] = $type;
        }
        if (!is_null($price)) {
            $data['price'] = $price;
        }
        if (!is_null($noteDate)) {
            $data['date'] = $noteDate;
        }
        if (!is_null($remark)) {
            $data['remark'] = $remark;
        }
        if (!is_null($isDelete)) {
            $data['isdelete'] = IS_DELETE;
        }

        $result = $this->where($options)->save($data);
        return $result === false ? false : true;
    }
}
